﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using sydfdl.TMS.BLL;
using sydfdl.TMS.Tool;
using sydfdl.TMS.Model;

namespace sydfdl.TMS.Areas.GoodsOrderMgr.Controllers
{
    /// <summary>
    /// 发货管理
    /// </summary>
    public class GoodsOrderController : Controller
    {
        BLLT_E_GoodsOrder bllOrder = new BLLT_E_GoodsOrder();

        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 创建发货单
        /// </summary>
        /// <returns></returns>
        public ActionResult CreateOrder()
        {
            string isMax = Request["isMax"];
            if (string.IsNullOrEmpty(isMax))
            {
                isMax = "0";
            }
            ViewBag.IsMax = isMax;

            return View();
        }

        /// <summary>
        /// 删除发货单
        /// </summary>
        /// <returns></returns>
        public ActionResult DeleteOrder()
        {
            return View();
        }

        /// <summary>
        /// 获取初次加载页面时所需要数据
        /// </summary>
        /// <returns></returns>
        public JsonResult GetAllIntiData()
        {
            return this.Json(new string[1], JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 根据手机号获取客户信息
        /// </summary>
        public void GetCustomerInfo()
        {
            try
            {
                string key = Request["key"];

                DataTable dt = bllOrder.GetCustomerInfoByPhone(key);

                string str = MiniUIJson.Encode(dt);

                Response.Write(str);
            }
            catch (Exception ex)
            { }
        }

        /// <summary>
        /// 获取目的地
        /// </summary>
        public void GetDestinationByCode()
        {
            try
            {
                string key = Request["key"];

                DataTable dt = bllOrder.GetDestinationByCode(key);

                string str = MiniUIJson.Encode(dt);

                Response.Write(str);
            }
            catch (Exception ex)
            {

            }
        }

        /// <summary>
        /// 获取付款方式
        /// </summary>
        public void GetFreightType()
        {
            try
            {
                DataTable dt = bllOrder.GetFreightType();

                string str = MiniUIJson.Encode(dt);

                Response.Write(str);
            }
            catch (Exception ex)
            {

            }
        }

        /// <summary>
        /// 获取提货方式
        /// </summary>
        public void GetReceiverGetGoodsType()
        {
            try
            {
                DataTable dt = bllOrder.GetReceiverGetGoodsType();

                string str = MiniUIJson.Encode(dt);

                Response.Write(str);
            }
            catch (Exception ex)
            {

            }
        }

        /// <summary>
        /// 获取所有卸货站
        /// </summary>
        public void GetAllCompany()
        {
            try
            {
                DataTable dt = bllOrder.GetAllCompany();

                string str = MiniUIJson.Encode(dt);

                Response.Write(str);
            }
            catch (Exception ex)
            {

            }
        }

        public void SaveGoodsOrder()
        {
            string msg = string.Empty;

            Dictionary<string, string> returnMsg = new Dictionary<string, string>();

            try
            {
                string json = @"[]";
                IEnumerable<T_E_GoodsOrder> orderMainData = Request.Form.Get("orderMainData") == json ? null : JsonHelp.ConvertJsonToEntity<IEnumerable<T_E_GoodsOrder>>(Request.Form.Get("orderMainData"));
                IEnumerable<T_E_GoodsOrder> orderFeeData = Request.Form.Get("orderFeeData") == json ? null : JsonHelp.ConvertJsonToEntity<IEnumerable<T_E_GoodsOrder>>(Request.Form.Get("orderFeeData"));
                IEnumerable<T_E_GoodsOrderDetail> goodgridData = Request.Form.Get("goodgridData") == json ? null : JsonHelp.ConvertJsonToEntity<IEnumerable<T_E_GoodsOrderDetail>>(Request.Form.Get("goodgridData"));

                List<T_E_GoodsOrder> order = new List<T_E_GoodsOrder>();

                T_E_GoodsOrder orderMain = new T_E_GoodsOrder();
                orderMain = orderMainData.FirstOrDefault() == null ? new T_E_GoodsOrder() : orderMainData.FirstOrDefault();

                T_E_GoodsOrder orderFee = new T_E_GoodsOrder();
                orderFee = orderFeeData.FirstOrDefault() == null ? new T_E_GoodsOrder() : orderFeeData.FirstOrDefault();

                orderMain.AccountsReceivable = orderFee.AccountsReceivable;
                orderMain.Issued = orderFee.Issued;
                orderMain.DeliveryFee = orderFee.DeliveryFee;
                orderMain.ReceiptFee = orderFee.ReceiptFee;
                orderMain.PaidGoodFee = orderFee.PaidGoodFee;
                orderMain.PaidFreight = orderFee.PaidFreight;
                orderMain.PaidOther = orderFee.PaidOther;
                orderMain.CollectionGoodCash = orderFee.CollectionGoodCash;
                orderMain.ReceiverGetGoodsType = orderFee.ReceiverGetGoodsType;
                orderMain.DeliveryReserved = orderFee.DeliveryReserved;
                orderMain.TransitReserved = orderFee.TransitReserved;
                orderMain.LandingFee = orderFee.LandingFee;
                orderMain.BasicCostFee = orderFee.BasicCostFee;
                orderMain.DeliveryCash = orderFee.DeliveryCash;
                orderMain.TicketCreatePlace = orderFee.TicketCreatePlace;
                orderMain.Salesman = orderFee.Salesman;
                orderMain.Remark = orderFee.Remark;

                orderMain.IsReceipt = orderFee.IsReceipt;
                orderMain.IsOwnLine = orderFee.IsOwnLine;
                orderMain.IsFeedBack = orderFee.IsFeedBack;
                orderMain.IsPaidGoodCash = orderFee.IsPaidGoodCash;
                orderMain.IsPaidFreight = orderFee.IsPaidFreight;
                orderMain.IsPaidOther = orderFee.IsPaidOther;

                orderMain.OrderStatus = new Guid();
                orderMain.PaymentStatus = new Guid();
                orderMain.PrecessBy = new Guid();
                orderMain.CurrentStationID = orderMain.SubCompanyID;

                order.Add(orderMain);

                returnMsg = bllOrder.SaveGoodsOrderInfo(order.AsEnumerable(), goodgridData, "me");

            }
            catch (Exception ex)
            {

            }

            Response.Write(MiniUIJson.Encode(returnMsg));
        }
    }
}
